import 'package:flutter/material.dart';
import 'package:flutter_basic/basic/basic_view.dart';
import 'package:flutter_basic/modules/home/controller.dart';
import 'package:flutter_basic/modules/main/controller.dart';
import 'package:flutter_basic/modules/trans/controller.dart';
import 'package:get/get.dart';

class MainPage extends BasicPage<MainPageController>{
  MainPage.bind(MainPageController controller, {String? tag}) : super.bind(controller, tag: tag);

  Widget build(BuildContext context) {
    print('build-build:${this.runtimeType}');
    HomeController homeController = Get.find(tag: viewTag);
    TransactionPageController transController = Get.find(tag: viewTag);
    return Scaffold(
      body: WillPopScope(
        onWillPop: (){
          if(controller.currentIndex.value == 0){
            if(homeController.currentPage > 0){
              homeController.pageController.previousPage(
                  duration: Duration(milliseconds: 200),
                  curve: Curves.ease,
              );
            }
          }else if(controller.currentIndex.value == 1){
            if(transController.currentPage > 0){
              transController.pageController.previousPage(
                duration: Duration(milliseconds: 200),
                curve: Curves.ease,
              );
            }
          }
          return Future.value(false);
        },
        child: Obx((){
          return IndexedStack(
            children: controller.pages,
            index: controller.currentIndex.value,
          );
        }),
      ),
      bottomNavigationBar: Obx((){
        return BottomNavigationBar(
          backgroundColor: Colors.white,
          type: BottomNavigationBarType.fixed,
          currentIndex: controller.currentIndex.value,
          selectedFontSize: 12,
          unselectedFontSize: 12,
          selectedItemColor: Color(0xFFBB2E2D),
          unselectedItemColor: Color(0xFFC9C9C9),
          onTap: controller.indexChanged,
          items: controller.bottomItems.map((item) => generateBottomBarItem(item)).toList(),
        );
      }),
    );
  }

  BottomNavigationBarItem generateBottomBarItem(Map<String, String> item){
    return BottomNavigationBarItem(
        icon: Image.asset(
          'images/bottom_item/${item['icon']}',
          height: 18,
          fit: BoxFit.fitHeight,
        ),
        activeIcon: Image.asset(
          'images/bottom_item/${item['active_icon']}',
          height: 18,
          fit: BoxFit.fitHeight,
        ),
        label: item['title'],
    );
  }

}